#pragma once

#include <iostream>
#include <string>
#include <vector>
#include <jsoncpp/json/json.h>
#include <cstdio>
#include <cstdlib>
#include <unistd.h>

#include "../httplib.h"
#include "./as_view.hpp"
#include "./as_model.hpp"

namespace as_control
{
    using namespace httplib;
    using namespace as_view;
    using namespace as_model;

    struct Machine{
        std::string ip="127.0.0.1";
        int port=8081;

        void set_machineConf()
        {
            //读取我们的主机配置文件，获得该服务所需要的ip和port
            FILE *f_r=fopen("../conf/service_machine.conf","r");
            if(!f_r){
                perror("[主机配置文件打开失败]");
                exit(0);
            }
            char result[100];
            std::string tmp1="";    //ip
            std::string tmp2="";    //port
            int flag=0;
            if(fgets(result,100,f_r)!=NULL){
                char *c=&result[0];
                while(*c!='\n')
                {
                    if(*c==':'){
                        flag=1;
                        c++;
                    }
                    if(flag==0){
                        tmp1+=*c;
                        c++;
                    }else{
                        tmp2+=*c;
                        c++;
                    }
                }
            }else{
                std::cout<<"[主机配置文件读取失败]"<<std::endl;
            }
            fclose(f_r);

            ip=tmp1;//"127.0.0.1"
            port=atoi(tmp2.c_str());//8081
        }
    };

    class control
    {
    public:
        view _view;   // 提供渲染功能
        model _model; // 提供后端数据(数据库)

    public:
        control()
        {
        }
        ~control()
        {
        }

    public:
        void analysis_text(std::string *html)
        {
            _view.analysis_text_html(html);
        }

        void analysis(const std::string in_json, std::string *out_json)
        {
            int cnt=0;
            while(1)
            {
                Machine m;
                std::cout<<"["<<"成功获取后端模型服务主机："<<m.ip<<":"<<m.port<<"]"<<std::endl;
                Client cli(m.ip, m.port);
                auto res=cli.Post("/emotion_analysis_model",in_json,"application/json;charset=utf-8");
                sleep(1);
                if(res){
                    std::cout<<"[后端模型服务成功]"<<std::endl;
                    *out_json=res->body;
                    break;
                }else{
                    cnt++;
                    if(cnt==5){
                        break;
                    }
                    std::cout<<"[后端模型服务失败]"<<std::endl;
                }
            }
        }
        
    public:
        void search_user(const std::string in_json, std::string *out_json)
        {
            int res=_model.search_mysql(in_json);
            Json::Value out_value;
            Json::StyledWriter writer;
            if(res==1){
                out_value["status"] = "success";
                *out_json = writer.write(out_value);
            }else{
                out_value["status"] = "fail";
                *out_json = writer.write(out_value);
            }
        }

        void import_corpus(const std::string in_json, std::string *out_json)
        {
            int res=_model.import_mysql(in_json);
            Json::Value out_value;
            Json::StyledWriter writer;
            if(res==1){
                out_value["status"] = "success";
                *out_json = writer.write(out_value);
            }else{
                out_value["status"] = "fail";
                *out_json = writer.write(out_value);
            }
        }
        
        void modify_corpus(const std::string in_json,std::string *out_json)
        {
            int res=_model.modify_mysql(in_json);
            Json::Value out_value;
            Json::StyledWriter writer;
            if(res){
                out_value["status"] = "success";
                *out_json = writer.write(out_value);
            }else{
                out_value["status"] = "fail";
                *out_json = writer.write(out_value);
            }
        }   

        void query_corpus(const std::string in_json, std::string *out_json)
        {
            std::string res=_model.query_mysql(in_json);
            Json::Value out_value;
            Json::StyledWriter writer;
            int len=res.size();
            if(len>0){
                out_value["status"] = res;
                *out_json = writer.write(out_value);
            }else{
                out_value["status"] = "fail";
                *out_json = writer.write(out_value);
            }
        }
        
    public:
        //2.接收到传来的json串后，继续传送到后端相应的数据处理模块中
        //6.接收查询到的相应语料，写入out_json串中
        void query_modelcorpus(const std::string in_json, std::string *out_json)
        {
            std::string res=_model.query_modeldatasets(in_json);
            Json::Value out_value;
            Json::StyledWriter writer;
            int len=res.size();
            if(len>0){
                out_value["status"] = res;
                *out_json = writer.write(out_value);
            }else{
                out_value["status"] = "fail";
                *out_json = writer.write(out_value);
            }
        }

        void modify_modelcorpus(const std::string in_json, std::string *out_json)
        {
            int res=_model.modify_modeldatasets(in_json); 
            Json::Value out_value;                               
            Json::StyledWriter writer;
            if(res==1){
                out_value["status"] = "success";                       
                *out_json = writer.write(out_value);             
            }else{
                out_value["status"] = "fail";
                *out_json = writer.write(out_value);             
            }   
        }

    public:
        void root_createuser(const std::string in_json,std::string *out_json)
        {
            int res=_model.create_sysuser(in_json);
            Json::Value out_value;                               
            Json::StyledWriter writer;
            if(res==1){
                out_value["status"]="success";
                *out_json=writer.write(out_value);
            }else{
                out_value["status"] = "fail";
                *out_json = writer.write(out_value);
            }
        }

        void root_deleteuser(const std::string in_json,std::string *out_json)
        {
            int res=_model.delete_sysuser(in_json);
            Json::Value out_value;                               
            Json::StyledWriter writer;
            if(res==1){            
                out_value["status"]="success";
                *out_json=writer.write(out_value);
            }else{
                out_value["status"] = "fail";
                *out_json = writer.write(out_value);
            } 
        }
    private:
    };
}
